package com.jyu.apts.service.impl;

import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.jyu.apts.dao.QRCodeRepository;
import com.jyu.apts.entity.QRCode;
import com.jyu.apts.service.QRCodeService;

@Service
public class QRCodeServiceImpl implements QRCodeService {
	
	@Autowired
	QRCodeRepository qrCodeDao;
	
	@Override
	public QRCode getOne(long id) {
		return qrCodeDao.findById(id).get();
	}

	@Override
	public Page<QRCode> getQRCodePage(Integer pageNumber, Integer pageSize,Integer type,Integer status, Long organizationId, String search) {
		if(pageNumber == null || pageNumber <= 0) {
			pageNumber = 1;
		}
		if(pageSize == null || pageSize <= 0) {
			pageSize = 10;
		}
		Sort sort = new Sort(Sort.Direction.ASC,"id");
		PageRequest pageRequest = new PageRequest(pageNumber-1,pageSize,sort);
		Specification<QRCode> spec = new Specification<QRCode>() {
			@Override
			public Predicate toPredicate(Root<QRCode> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				Predicate pre = criteriaBuilder.conjunction();
				if(type!=null) {
					pre = criteriaBuilder.and(pre,criteriaBuilder.equal(root.get("type"), type));
				}
				if(status!=null) {
					pre = criteriaBuilder.and(pre,criteriaBuilder.equal(root.get("status"), status));
				}
				if(organizationId != null) {
					pre = criteriaBuilder.and(pre,criteriaBuilder.equal(root.get("author").get("organization").get("id"), organizationId.longValue()));
				}
				if(search!=null && search.trim().length()!=0) {
					Predicate or_pre = criteriaBuilder.disjunction(); 
					or_pre =criteriaBuilder.or(or_pre,criteriaBuilder.like(root.get("qrCodeTemplate").get("name"), "%"+search+"%"));
					or_pre =criteriaBuilder.or(or_pre,criteriaBuilder.like(root.get("dataValues"), "%"+search+"%"));
					pre = criteriaBuilder.and(pre,or_pre);
				}
				return pre;
			}
			
		};
		return qrCodeDao.findAll(spec, pageRequest);
	}

	@Override
	public void save(QRCode qrCode) {
		qrCodeDao.saveAndFlush(qrCode);
	}

	@Override
	public void save(List<QRCode> qrCodes) {
		qrCodeDao.saveAll(qrCodes);
		qrCodeDao.flush();
	}

	@Override
	public void delete(List<QRCode> qrCodes) {
		qrCodeDao.deleteAll(qrCodes);
	}

	@Override
	public void delete(QRCode qrCode) {
		qrCodeDao.delete(qrCode);
	}

	@Override
	public List<QRCode> findAll() {
		return qrCodeDao.findAll();
	}

	@Override
	public List<QRCode> findAllById(List<Long> ids) {
		return qrCodeDao.findAllById(ids);
	}

	@Override
	public QRCode findByCode(String code) {
		return qrCodeDao.findByCode(code);
	}

}
